package com.xiake.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiake.pojo.Headline;
import com.xiake.pojo.vo.PortalVo;
import com.xiake.service.HeadlineService;
import com.xiake.mapper.HeadlineMapper;
import com.xiake.utils.JwtHelper;
import com.xiake.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 周清峰
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-03-12 14:19:43
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    HeadlineMapper headlineMapper;
    @Autowired
    JwtHelper jwtHelper;
//    @Autowired
//    Headline headline;
    @Override
    public Result findNewPage(PortalVo portalVo) {
        //利用分页插件创建分页对象
        //设置当前显示页和显示容量
        IPage<Map> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        //传入分页对象能在sql语句末尾加上limit x,y  x为当前页，y为当前页显示的行数
        headlineMapper.selectMyPage(page, portalVo);
        //把records数据和得到的信息数据封装到一个map中
        Map<String, Object> data = new HashMap<>();
        data.put("pageData",page.getRecords());
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getPages());
        data.put("totalSize",page.getTotal());
        HashMap<String, Object> pageInfo = new HashMap<>();
        pageInfo.put("pageInfo",data);
        return Result.ok(pageInfo);
    }

    @Override
    public Result showHeadlineDetail(Integer hid) {
        //根据前端发过来的hid值查询数据,自定义多表查询
        Map data=headlineMapper.selectDetailMap(hid);
       //把数据封装到map中进行返回
         Map<String, Object> map = new HashMap<>();

        //更改数据库中这个id的文章的浏览量
        Headline headline = new Headline();
        //按照id进行修改
        headline.setHid(hid);

        //因为设置了乐观锁，必须根当前数据库版本号一样的才能进行修改，headline默认的vision=0
        headline.setVersion((Integer) data.get("version"));
        //浏览量+1
        Object pageViews = data.get("pageViews");
        headline.setPageViews((Integer) pageViews+1);
        headlineMapper.updateById(headline);
        map.put("headline",data);
        return Result.ok(map);
    }

    //一般来说只有登录过的用户才会携带token,所以要验证用户是否登录
    //方法一是在在做业务方法之前做一个token过期检验
    //方法二是配置一个拦截器，对请求头中的token进行解析，并决定是否放行
    @Override
    public Result publish(Headline headline, String token) {
        //因为外部传进的headline对象只有部分属性，所以要补全数据
        //根据token找到哪个用户发布的文章
        int userId = jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        //补全数据
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        headline.setPageViews(0);
        //插入
        headlineMapper.insert(headline);
        return Result.ok(null);
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        //根据id返回信息
        Headline headline = headlineMapper.selectById(hid);
        //封装成map返回给前端
        HashMap<String, Object> data = new HashMap<>();
        data.put("headline",headline);
        return Result.ok(data);
    }

    @Override
    public Result updateData(Headline headline) {
        //根据id拿到当前头条的版本号
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        //将此headline对象的版本号设置一样，因为有乐观锁
        headline.setVersion(version);
        //修改更新时间
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);
        return Result.ok(null);
    }


}




